<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="标题:" prop="title">
        <el-input v-model="ruleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="分类栏目"  prop="region">
        <el-select v-model="ruleForm.region" placeholder="请选择分类栏目">
          <el-option
            v-for="item in typelist"
            :key="item.id"
            :label="item.typename"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序值:">
        <el-input v-model="ruleForm.sort" type="number"></el-input>
      </el-form-item>
      <el-form-item label="关键词:">
        <el-input v-model="ruleForm.keywords"></el-input>
      </el-form-item>
      <el-form-item  prop="content">
        <quill-edit class="article_content_item" ref="myTextEditor"
                     :fileName="myFile"
                     :canCrop="canCrop"
                     :uploadurl="uploadurl"
                    @input="handleInput"
                     v-model="content"></quill-edit>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>

    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      center
    >
      <span style="width:100%;text-align: center">{{ msg }}</span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
          </span>
    </el-dialog>
  </div>
</template>

<script>
import QuillEdit from '@/components/editor/QuillEditor'

export default {
  name: 'add-news',
  components: {
    QuillEdit
  },
  data () {
    return {
      myFile: '',
      typelist: [],
      dialogVisible: false,
      msg: '',
      uploadurl: 'http://www.yuzhekeji.club:8080/upload/uploadImg',
      content: '',
      canCrop: false, // 是否可以裁剪
      ruleForm: {
        title: '',
        region: '',
        sort: 50,
        keywords: '',
        content: '',
        dialogImageUrl: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, max: 40, message: '长度在 5 到 40 个字符', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入资讯内容', trigger: 'blur' }
        ],
        region: [
          { required: true, message: '请选择分类栏目', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    handleInput (res) {
      this.ruleForm.content = res
    },
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.saveArticle()
        } else {
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    getTypeList () {
      this.$http.get('/news/getNewsTypeList')
        .then(this.getTypeListInfoSucc)
    },
    getTypeListInfoSucc (res) {
      let data = res.data
      if (data.status === 200) {
        this.typelist = data.data
      }
    },
    saveArticle () {
      var params = new URLSearchParams()
      params.append('title', this.ruleForm.title)
      params.append('type', this.ruleForm.region)
      params.append('sort', this.ruleForm.sort)
      params.append('keywords', this.ruleForm.keywords)
      params.append('fileList', this.ruleForm.fileList)
      params.append('content', this.ruleForm.content)
      this.$http.post('/news/saveNews', params).then(res => {
        let data = res.data
        this.dialogVisible = true
        if (data.status === 200) {
          this.msg = '添加成功'
          this.content = ''
          this.ruleForm.keywords = ''
          this.ruleForm.title = ''
        } else {
          this.msg = data.msg
        }
      })
    }
  },
  mounted () {
    this.getTypeList()
  }
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
.article_content_item
  line-height 24px
</style>
